<template>
  <div class="marketHeader">
    <div class="navigator">
      <div class="left">
        <router-link to="/">校园二手交易网站</router-link>
      </div>
      <div class="right">
        <!-- 登录之前 -->
        <router-link to="/login"
                     v-if="!isLogin">登录/注册</router-link>
        <!-- 登录之后 -->
        <router-link to="/"
                     class="login"
                     tag="div"
                     v-else>
          已登录：{{ username }}&nbsp;<i class="el-icon-arrow-down"></i>
          <div class="personal">
            <router-link to="/userInfo">个人中心</router-link>
            <router-link to="/"><span @click="exit">退出登录</span></router-link>
          </div>
        </router-link>
        <router-link to="/cart"
                     target="_blank"><i class="el-icon-shopping-cart-1"></i>购物车</router-link>
        <router-link to="/collect"
                     target="_blank"><i class="el-icon-star-on"></i>收藏夹</router-link>
        <router-link :to="{path:'/chat',query:{chat_name:username}}"
                     target="_blank">消息</router-link>
        <router-link to="/community"
                     target="_blank">社区</router-link>
        <router-link to="/seller"
                     target="_blank">卖家中心</router-link>
        <router-link v-if="isAdmin"
                     to='/admin'
                     target="_blank">管理员</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isLogin: false,
      username: '',
      isAdmin: false,
    };
  },
  created () {
    // 检测用户是否处于登录状态
    let token = localStorage.getItem('token');
    if (token) {
      this.isLogin = true;
      this.username = localStorage.getItem('username');
    }
    this.$http.post('http://localhost:3000/userInfo', { username: localStorage.getItem('username') }).then(response => {
      if (response.data.data.userAdmin) {
        this.isAdmin = true;
      }
    })
  },
  methods: {
    exit () {
      this.isLogin = false;
      localStorage.removeItem('token');
    }
  }
}
</script>

<style lang="less" scoped>
.marketHeader {
  width: 100%;
  height: 36px;
  font-size: 14px;
  background-color: #dcdfe6;

  .navigator {
    width: 1100px;
    height: 36px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    a,
    .login {
      display: inline-block;
      height: 36px;
      line-height: 36px;
      padding: 0 10px;
    }
    .login {
      position: relative;
      cursor: pointer;
    }
    a:hover {
      color: #f10;
      background-color: #fff;
    }
    .login:hover {
      color: #f10;
      background-color: #fff;
    }
    .login:hover .personal {
      display: block;
      background-color: #fff;
    }
    .personal {
      display: none;
      z-index: 5;
      position: absolute;
      left: 0;
    }
    .personal a:hover {
      width: 100%;
      background-color: #dcdfe6;
      padding-right: 20px;
      box-sizing: border-box;
    }
    i {
      margin-right: 2px;
    }
  }
}
</style>